﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoPartsSearch.Web.ViewModels.Home;
using AutoPartsSearch.Domain.Abstract.Repositories;
using AutoPartsSearch.Domain.Abstract;

namespace AutoPartsSearch.Web.Controllers
{
    public class HomeController : Controller
    {
        private readonly IContactRepository _contactRepository;
        private readonly IMessageBuilder _messageBuilder;
        private readonly IMessageSender _messageSender;

        public HomeController(IContactRepository contactRepository, IMessageBuilder messageBuilder, IMessageSender sender)
        {
            _contactRepository = contactRepository;
            _messageBuilder = messageBuilder;
            _messageSender = sender;
        }

        public ActionResult HowItWorks()
        {
            return View();
        }

        public ActionResult Contact()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Contact(Contact contact)
        {
            if (!ModelState.IsValid)
            {
                return View(contact);
            }
            var dbContact = AutoMapper.Mapper.Map<AutoPartsSearch.Domain.Entities.Contact>(contact);
            _contactRepository.Add(dbContact);
            var message = _messageBuilder.BuildContactMessage(dbContact);
            _messageSender.Send(message);
            return RedirectToAction("Index", "SearchQuery");
        }

    }
}
